﻿using System;
using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.EntityRepository;
using KLS.PPDS.Resources;

namespace KLS.PPDS.ApplicationService.Manager
{
    public class CompanyManager : RepositoryManager<CompanyRepository>
    {       
/// <summary>
/// Return all available company which is not deleted
/// </summary>
/// <returns></returns>
/// 
        public static List<Company> GetAllCompanyGraphicsUserId(int userId) 
        {
            var com = Repository.GetAllCompany();

            return com.Where(c => c.Graphic == userId).OrderBy(c=>c.CompanyName).ToList();
        }

        public static List<Company> GetAllCompany()
        {
            return Repository.GetAllCompany();
        }

        public static List<Company> GetAllCompany(int id)
        {
            return Repository.GetAllCompany(id);
        }

        public static Company GetCompany(int companyId)
        {

            return Repository.GetCompany(companyId);
        }


        public static Company GetCompanyByName(string companyName)
        {
            
            return Repository.GetCompany(companyName);
            
        }
        public static Company GetCompanyByNo(string companyNo)
        {
           
            return Repository.GetCompanyByNo(companyNo);

        }

        public static string Validate(Company company)
        {
            var id = company.Id;
            if (id == 0) 
            {
                var com = GetCompanyByName(company.CompanyName);
                if (com != null && com.Id > 0) 
                {
                    if(com.IsDeleted)
                    {
                        return CommonMessage.CompanyExistsButDeleted;
                    }

                    return CommonMessage.CompanyExists;
                }
            }
            return String.Empty;
        }

        public static void Save(Company company)
        {
            var id = company.Id;   
            company.InitAudit();
            
            //if (id == 0) 
            //{
            //    var com = GetCompanyByName(company.CompanyName);
            //    if (com != null && com.Id > 0) 
            //    {
            //        throw new Exception(CommonMessage.CompanyExists);
            //    }
            //}

            Repository.SaveAndEvict(company);            
            if (id == 0)
            {
                LogManager.LogDbAdd(company);
            }
            else
            {
                LogManager.LogDbUpdate(company);
            }
        }
        public static void Update(Company company)
        {
            
            company.InitAudit();
            Repository.Update(company);
            LogManager.LogDbUpdate(company);
            
        }
        public static bool DeleteCompany(int companyId)
        {
          
            var company = Repository.FindOne(companyId);

            if (company != null)
            {
                company.InitAudit();
                company.IsDeleted = true;                
                Repository.Update(company);
                LogManager.LogDbDelete(company);
                return true;
            }
            
            return false;
        }

        public static User GetEditorUserByCompanyId(int companyId) 
        {
            var user = new User();

            var company = Repository.FindOne(companyId);

            if (company != null && company.Editor.HasValue) 
            {
                user = UserManager.Get(company.Editor.Value);
            }

            return user;
        }

        public static User GetGraphicsUserByCompanyId(int companyId)
        {
            var user = new User();

            var company = Repository.FindOne(companyId);

            if (company != null && company.Graphic.HasValue)
            {
                user = UserManager.Get(company.Graphic.Value);
            }

            return user;
        }
    }
}
